<template>
    <div class="addCoupon text">
        <div class="big">
            <!--头部—添加优惠券-->
            <div class="top">
                <div class="Leftgreen"></div>
                <div class="other">
                    &nbsp;添加优惠券
                    <div class="Topbutton">
                        <el-button class="el-button" @click="refresh">
                            <i class="el-icon-refresh"></i>
                            刷新
                        </el-button>
                        <el-button class="el-button" @click="back">
                            <i class="el-icon-arrow-left"></i>
                            返回
                        </el-button>
                    </div>
                </div>
            </div>
            <!--添加优惠券—信息填写-->
            <div class="addhead">
                &nbsp;&nbsp;
                <span class="text2 line">&nbsp;添加优惠券</span>
            </div>
            <!--添加优惠券—表单内容-->
            <div class="addInformation">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm addForm">
                    <el-form-item label="优惠券编号:" prop="couponCode">
                        <el-input class="ckInput" v-model="ruleForm.couponCode" autocomplete="off"></el-input>
                            <!--<template slot-scope="scopes">
                                {{ scopes.$index+1 }}
                            </template>-->
                    </el-form-item>
                    <el-form-item label="优惠券名称:" prop="couponName">
                        <el-input v-model="ruleForm.couponName" ></el-input>
                    </el-form-item>
                    <el-form-item label="开始时间:" required>
                        <el-col :span="24">
                            <el-form-item prop="startDate">
                                <el-date-picker type="date" placeholder="请选择时间" v-model="ruleForm.startDate" style="width: 70%;"></el-date-picker>
                            </el-form-item>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="结束时间:" required>
                        <el-col :span="24">
                            <el-form-item prop="endDate">
                                <el-date-picker type="date" placeholder="请选择时间" v-model="ruleForm.endDate" style="width: 70%;"></el-date-picker>
                            </el-form-item>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="上线/下线:" prop="couponsOnline">
                        <el-radio-group v-model="ruleForm.couponsOnline" >
                            <el-radio label="上线"></el-radio>
                            <el-radio label="下线"></el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="发放数量:" prop="issueQuantity" style="width: 59%">
                        <el-input v-model="ruleForm.issueQuantity"></el-input>
                    </el-form-item>
                    <el-form-item label="发放方式:" prop="couponsGrant">
                        <el-select v-model="ruleForm.couponsGrant" placeholder="请选择发放方式">
                            <el-option label="自动发放" value="2"></el-option>
                            <el-option label="活动领取" value="3"></el-option>
                            <el-option label="人工发放" value="4"></el-option>
                            <el-option label="无限制" value="5"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="备注">
                        <el-input class="textarea" rows="5" type="textarea" v-model="ruleForm.couponsRemarks"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">立即提交</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "addCoupon",
        data() {
            return {
                //表单数据
                ruleForm: {
                    couponCode:'',
                    couponName:'',
                    startDate:'',
                    endDate:'',
                    couponsOnline:'',
                    issueQuantity:'',
                    couponsGrant:'',
                    couponsRemarks:''
                },
                //表单填写提醒
                rules: {
                    couponCode: [
                        { required: true, message: '请输入优惠券编号，注意不要重复', trigger: 'blur' }
                    ],
                    couponName: [
                        { required: true, message: '请输入优惠券名称', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    couponsOnline: [
                        { required: true, message: '请选择优惠券状态', trigger: 'change' }
                    ],
                    startDate: [
                        { type: 'date', required: false, message: '请选择开始日期', trigger: 'change' }
                    ],
                    endDate: [
                        { type: 'date', required: true, message: '请选择结束日期', trigger: 'change' }
                    ],
                    couponsGrant: [
                        { required: true, message: '请至少选择一个发放方式', trigger: 'change' }
                    ],
                    issueQuantity: [
                        { required: true, message: '请填写发放数量', trigger: 'change' }
                    ],
                    couponsRemarks: [
                        { required: true, message: '请填写备注', trigger: 'blur' }
                    ],
                }
            };
        },
        methods:{
            //刷新页面按钮方法
            refresh(){
                window.location.reload();
            },
            //提交表单方法
            submitForm(ruleForm) {
                this.$refs[ruleForm].validate(async (valid) => {
                    if (valid) {
                        if (this.ruleForm.couponsOnline == '上线'){
                            this.ruleForm.couponsOnline = 2
                        }else if(this.ruleForm.couponsOnline == '下线'){
                            this.ruleForm.couponsOnline = 3
                        }
                        this.$http.post('/coupons/addCouponslist', {
                            "couponCode":this.ruleForm.couponCode,
                            "couponName":this.ruleForm.couponName,
                            "startDate":this.ruleForm.startDate,
                            "endDate":this.ruleForm.endDate,
                            "couponsOnline":this.ruleForm.couponsOnline,
                            "issueQuantity":this.ruleForm.issueQuantity,
                            "couponsGrant":this.ruleForm.couponsGrant,
                            "couponsRemarks":this.ruleForm.couponsRemarks
                        })
                        alert('添加成功!');
                        window.location.href="http://localhost:8080/"
                    } else {
                        console.dir('添加失败!!');
                        return false;
                    }
                });
            },
            //重置方法
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            //返回方法
            back(){
                this.$router.back()
            }
        }
    }
</script>

<style scoped>
    .addCoupon{
        width: 100%;
        height: 100%;
    }
    .big{
        background-color: #F6F7F9;
        height: 100%;
        width: 100%;
    }
    .top{
        background-color: #F3F3F3;
        border: 1px #d5d5d5 solid;
        height: 40px;
        line-height: 40px;
    }
    .Leftgreen{
        background-color: #1ABC9C;
        width: 4px;
        height: 19px;
        float: left;
        margin-top: 11px;
    }
    .text{
        color: #978f96;
        font-family: Arial;
    }
    .text2{
        color: #5C6573;
    }
    .Topbutton{
        float: right;
        margin-right: 40px;
        margin-top: 1.5px;
    }
    .el-button{
        margin-right: 15px;
        height: 30px;
        Padding-top:8px
    }
    .addhead{
        margin-top: 20px;
        width: 1080px;
        height: 40px;
        background-color: #F3F3F3;
        border: 1px #d5d5d5 solid;
        line-height: 40px;
    }
    .addInformation{
        background-color: white;
        height: 700px;
        width: 1080px;
        border: 1px #d5d5d5 solid;
    }
    .addForm{
        width: 500px;
        margin: 40px auto;
    }
</style>